package cn.kgc.ssm.service.impl;

import cn.hutool.crypto.digest.MD5;
import cn.kgc.ssm.commons.ResponseResult;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.form.LoginForm;
import cn.kgc.ssm.mapper.UserMapper;
import cn.kgc.ssm.service.UserService;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/5/8
 * @description
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;


    @Override
    public ResponseResult login(HttpServletRequest request, LoginForm loginForm) {

        // 验证码
        String captcha = loginForm.getCaptcha();
        HttpSession session = request.getSession();
        String code = (String) session.getAttribute("code");

        if (!StringUtils.equals(captcha,code)){
            return  ResponseResult.fail().message("验证码错误");
        }

        // 用户名
        String username = loginForm.getUsername();
        User user =   userMapper.selectByUserName(username);
        if (ObjectUtils.isEmpty(user)){
            return ResponseResult.fail().message("用户名或密码错误");
        }

        // 密码
        String formPassword = loginForm.getPassword();   //  明文   123456
        String DBPassword = user.getPassword();  //  49ba59abbe56e057
        if (!StringUtils.equals(MD5.create().digestHex16(formPassword),DBPassword )){
            return ResponseResult.fail().message("用户名或密码错误");
        }


        // 将用户信息缓存在 session中 用于后期非法请求拦截业务
        session.setAttribute("user",user);

        return ResponseResult.success().message("认证通过").data(user);
    }

    @Override
    public ResponseResult getPersonalInfo(String username) {

        User user = userMapper.selectByUserName(username);

        return ResponseResult.success().data(user);
    }

    @Override
    public ResponseResult editPersonalInfo(User user) {
         userMapper.editPersonalInfo(user);
        return ResponseResult.success().message("个人信息编辑成功");
    }

    @Override
    public ResponseResult editPersonalPassword(String oldPassword, String password, HttpServletRequest request) {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        // 1. 判定当前密码是否正确
        String username = user.getUsername();
        String DBPassword = userMapper.selectByUserName(username).getPassword();
        String formPassword = MD5.create().digestHex16(oldPassword);
        if (!StringUtils.equals(DBPassword,formPassword)){
            return ResponseResult.fail().message("原始密码不正确");
        }
        // 2.修改密码
        String newPassword = MD5.create().digestHex16(password);

        userMapper.editPersonalPassword(newPassword,user.getId());

        return ResponseResult.success().message("密码修改成功");
    }

    @Override
    public ResponseResult userPage(Integer page, Integer limit, String nickname) {

        // 1.开启分页
        PageHelper.startPage(page,limit);
        List<User> users = userMapper.searchUsers(nickname);
        PageInfo<User> userPageInfo = new PageInfo<User>(users);
        long total = userPageInfo.getTotal();

        return ResponseResult.success().data(users).count(total);
    }

    @Override
    public ResponseResult addUser(User user) {

        // 默认密码
        String defaultPassword="111111";
        String Md5Password = MD5.create().digestHex16(defaultPassword);
        user.setPassword(Md5Password);

        // 根据用户名查询用户是否存在
        User usr = userMapper.selectByUserName(user.getUsername());
        if (!ObjectUtils.isEmpty(usr)){
            return  ResponseResult.fail().message("用户名已存在");
        }

        userMapper.insertUser(user);
        return ResponseResult.success().message("添加成功");
    }

    @Override
    public ResponseResult getByUserId(Integer id) {
         User user =  userMapper.selectById(id);
        return ResponseResult.success().data(user);
    }

    @Override
    public ResponseResult editUser(User user) {

        userMapper.updateUser(user);

        return ResponseResult.success().message("更新成功");
    }

    @Override
    public ResponseResult removeUser(List<Integer> ids) {

        userMapper.deleteUser(ids);
        return ResponseResult.success().message("删除成功");
    }

    @Override
    public ResponseResult resetPassword(Integer id) {

        String password = "111111";
        String md5Password = MD5.create().digestHex16(password);

        userMapper.updatePassword(id,md5Password);
        return ResponseResult.success().message("密码重置成功");
    }
}
